#!/Users/timmenzies/opt/gawk/bin/gawk -f 
 
# wilcoxon.awk
# Revision: 296
# Built by timmenzies on Fri Jul 29 20:43:58 PDT 2011.

#   _ _ _                                               ((
#  |=|=|=|.----------------------------------------.    _))_
#  |-|-|-|| AWK code of a tiny book on data mining | ___\__/)_
#  |_|_|_|'----------------------------------------| |_|_~~_|_|

# This file is part of TINY.
#
# TINY is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# TINY is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
#  along with TINY.  If not, see <http://www.gnu.org/licenses/>.

 @include "lib.awk"
 function wilcoxon(pop1,pop2,up,critical,	\
		   ranks,w0,w,correction,z,sigma,\
                   i, delta, n,diff,absDiff) {
     for(i in pop1) {
	 	delta = pop1[i] - pop2[i]
	 	if (delta) { 
	    	 n++
	     	diff[i]    = delta
	     	absDiff[i] = abs(delta) 
	 }}
     rank(absDiff,ranks)
     for(i in absDiff)  {
	 	w0 = ranks[absDiff[i]] 
	 	w += (diff[i] < 0)  ? -1*w0  :  w0
     }
     sigma = sqrt((n*(n+1)*(2*n+1))/6)
     z = (w - 0.5) / sigma; 
     if (z >= 0 && z <= critical) 
	 	return 0
     else  
	 	return up ? w : -1*w
 }

